package com.tjetc.impl;

import com.tjetc.dao.ResidentMapper;

import com.tjetc.domian.Resident;
import com.tjetc.service.ResidentService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class ResidentServiceImpl implements ResidentService {
    private ResidentMapper residentMapper = Db.getMapper(com.tjetc.dao.ResidentMapper.class);
    private Result result=new Result();

    @Override
    public List<Resident> select() {

        return residentMapper.select();
    }

    @Override
    public Result update(Resident resident) {
        try {
           Resident resident1 = residentMapper.selectByPrimaryKey(resident.getResidentid());
           if (resident1==null){
               return  result.setCode(400).setMsg("该住户不存在").setData(null);
           }else{
               int i = residentMapper.updateByPrimaryKeySelective(resident);
               return i==1?result.setCode(200).setMsg("修改成功").setData(resident):result.setCode(500).setMsg("修改失败").setData(null);
           }

        } catch (Exception e) {
            e.printStackTrace();
            return result.setCode(500).setMsg("修改失败").setData(null);

        }
    }

    @Override
    public Result del(int id) {
       int rows= residentMapper.deleteByPrimaryKey(id);
       return rows==1?result.setCode(200).setMsg("删除成功").setData(null):result.setCode(500).setMsg("删除失败").setData(null);

    }

    @Override
    public Result add(Resident resident) {
      int rows=  residentMapper.insert(resident);
      return rows==1?result.setCode(200).setMsg("添加成功").setData(resident):result.setCode(500).setMsg("添加失败").setData(null);
    }

    @Override
    public int getApartmentId(int residentId) {
        return  residentMapper.getApartmentId(residentId);
    }

    @Override
    public Result getApartmentIdByname(String name) {
        try {
            int i = residentMapper.getApartmentIdByname(name);
            return result.setCode(200).setMsg("查询成功").setData(i);
        } catch (Exception e) {
            e.printStackTrace();
            return result.setCode(500).setMsg("查询失败").setData(null);
        }
    }

}
